package com.zhengke.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.dao.PurchasingDao;
import com.zhengke.pojo.CommonGoodsHeart;
import com.zhengke.pojo.Result;
import com.zhengke.pojo.TvPurchase;
import com.zhengke.pojo.TvPurchaseDetails;
import com.zhengke.service.PurchasingService;

/**  

* 创建时间：2020年1月6日 下午3:38:19  

* 项目名称：tvmall  

* @author 刘浩  

* @version 1.0     

* 文件名称：PurchasingServiceImpl.java   

* 类说明：  

*/
@Service
public class PurchasingServiceImpl implements PurchasingService  {
	@Autowired
	PurchasingDao pd;
	Result rs = new Result();
	@Override
	public Result selectTvPurchase(TvPurchase tp) {
		
		PageHelper.startPage(tp.getPage(),tp.getLimit(),true);
		List<TvPurchase> selectTvPurchaseList = pd.selectTvPurchase(tp);
		PageInfo<TvPurchase> pageInfo = new PageInfo<TvPurchase>(selectTvPurchaseList);
		rs.setCount(Integer.parseInt(String.valueOf(pageInfo.getTotal())));
		rs.setData(selectTvPurchaseList);
		rs.setCode(0);
		rs.setMsg("success");
		return rs;
	}
	/**
	 * 	批量删除
	 */
	@Override
	public int deleteTvPurchase(String id) {
		String[] purchaseId = id.split(",");
		int deleteBaleGoods = pd.deleteTvPurchase(purchaseId);
		return deleteBaleGoods;
	}
	/**
	 * 确认采购单
	 */
	@Override
	public int confirmTvPurchase(String purchaseId) {
		int confirmTvPurchase = pd.confirmTvPurchase(purchaseId);
		return confirmTvPurchase;
	}
	@Override
	public Result selectTvPurchaseDetails(TvPurchaseDetails tpd) {
		List<TvPurchaseDetails> selectTvPurchaseDetailsList = pd.selectTvPurchaseDetails(tpd);
		
		rs.setData(selectTvPurchaseDetailsList);
		rs.setCode(0);
		rs.setMsg("success");
		return rs;
	}
	/**
	 * 根据服务点查询其拥有的商品
	 */
	@Override
	public Result selectCommonGoodsHeart(CommonGoodsHeart cg) {
		String num = cg.getNum();
		
		Integer goodsCommonId = cg.getGoodsCommonId();
		System.out.println(goodsCommonId);
		PageHelper.startPage(cg.getPage(), cg.getLimit(),true);
		List<CommonGoodsHeart> selectCommonGoodsHeartList = pd.selectCommonGoodsHeart(cg);
		for(int i=0;i<selectCommonGoodsHeartList.size();i++) {
			if(selectCommonGoodsHeartList.get(i).getGoodsCommonId().equals(goodsCommonId)) {
				System.out.println(selectCommonGoodsHeartList.get(i).getGoodsCommonId());
				selectCommonGoodsHeartList.get(i).setNum(num);
			}
			
		}
		PageInfo<CommonGoodsHeart> pageInfo = new PageInfo<CommonGoodsHeart>(selectCommonGoodsHeartList);
		rs.setCount(Integer.parseInt(String.valueOf(pageInfo.getTotal())));
		rs.setData(selectCommonGoodsHeartList);
		rs.setCode(0);
		rs.setMsg("success");
		return rs;
	}
	/**
	 *  增加订单
	 */
	@Override
	public int addTvPurchase(TvPurchase tp) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String format = df.format(new Date());
		tp.setCreateTime(format);
		short s = 10;
		tp.setStatus(s);
		int addTvPurchase = pd.addTvPurchase(tp);
		return addTvPurchase;
	}
	/**
	 *  增加订单详情
	 */
	@Override
	public int addTvPurchaseDetails(TvPurchaseDetails tpd) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String format = df.format(new Date());
		tpd.setCreateTime(format);
		short s = 0;
		tpd.setStatu(s);
		int addTvPurchaseDetails = pd.addTvPurchaseDetails(tpd);
		return addTvPurchaseDetails;
	}
	/**
	 * 修改采购单详情数量
	 */
	@Override
	public int UpdateTvPurchaseDetails(TvPurchaseDetails tpd) {
		int updateTvPurchaseDetails = pd.UpdateTvPurchaseDetails(tpd);
		return updateTvPurchaseDetails;
	}
	/**
	 * 	增加订购管理临时表
	 */
	@Override
	public int addTvPurchaseTemporary(TvPurchaseDetails tpd) {
		int num = pd.addTvPurchaseTemporary(tpd);
		return num;
	}

}
